package com.zhaopin.thrift;

import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;
import thrift.generated.Person;
import thrift.generated.PersonService;

/**
 * @author yin.peide
 * @date 2021-12-09 16:14
 */
public class ThriftClient {

    public static void main(String[] args) throws TTransportException {
        // 这一个跟服务端是呼应的
        TTransport transport = new TFramedTransport(new TSocket("localhost",8899),600);
        //这一个跟服务端也是呼应的 必须要对应上
        TProtocol protocol = new TCompactProtocol(transport);
        //生成一个client 对象，对服务端的内容进行调用
        PersonService.Client client = new PersonService.Client(protocol);

        try{
            transport.open();
            //执行调用方法
            Person person = client.getPersonByUsername("张三");
            System.out.println(person.getUsername() + " "+ person.isMarried() + " " + person.getAge() );
            System.out.println("-----------------------------------------------------------------------");

            Person person1 = new Person();
            person1.setUsername("李四");
            person1.setAge(60);
            person1.setMarried(true);
            client.savePerson(person1);

        }catch (Exception e){
            throw new RuntimeException(e.getMessage(),e);
        }finally {
            transport.close();
        }

    }
}
